package com.example.attendancesystem.Service.Imp;


import com.example.attendancesystem.Mapper.AdminsMapper;
import com.example.attendancesystem.Mapper.EmployeesMapper;
import com.example.attendancesystem.Models.Admin;
import com.example.attendancesystem.Models.Employee;
import com.example.attendancesystem.Service.LoginAndRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class LoginAndRegisterImpService implements LoginAndRegisterService {

    @Autowired
    private EmployeesMapper employeesMapper;

    @Autowired
    private AdminsMapper adminsMapper;

    ArrayList numberCount = new ArrayList();

    public Integer loginCount = 0;

    /**
     * 学生登录功能
     *
     * @param userNumber 工号
     * @param userPass   密码
     * @return 1001 密码正确返回主界面 ， 1002 密码错误 ， 1003 学号不存在 ，1004 登录失败
     */
    public int userLogin(String userNumber, String userPass, HttpSession session) {
        int code;
        try {
            //查询是否已经登录
            int numberSum = Collections.frequency(numberCount, userNumber);
            if (numberSum != 0) {
                code = 1007;
            } else {
                List<Employee> employees = employeesMapper.getEmpByEmpNumber(userNumber);
                //如果正常查询的话 只有一条记录 因为工号是唯一的
                if (employees.size() == 1) { // 查询到一条记录 再进行密码匹配
                    if (userPass.equals(employees.get(0).getEmpPassword())) { //密码正确
                        session.setAttribute("emp", employees.get(0)); // 将登录员工信息保存在Session中 取名 emp
                        session.setMaxInactiveInterval(60 * 60); //设置Session最大有效时间 单位秒
                        numberCount.add(userNumber);
                        loginCount += 1;
                        ServletContext application = session.getServletContext();
                        application.setAttribute("loginCount", loginCount);
                        code = 1001;
                    } else {
                        code = 1002; //密码错误
                    }
                } else {
                    List<Admin> admins = adminsMapper.getAdminByNumber(userNumber);
                    if (admins.size() == 1) {
                        if (userPass.equals(admins.get(0).getAdminPass())) {
                            session.setAttribute("admin", admins.get(0));
                            session.setMaxInactiveInterval(60 * 60);//设置Session最大有效时间 单位秒
                            numberCount.add(userNumber);
                            loginCount += 1;
                            ServletContext application = session.getServletContext();
                            application.setAttribute("loginCount", loginCount);
                            code = 1008;
                        } else {
                            code = 1002;
                        }
                    } else {
                        code = 1003;
                    }
                }
            }
        } catch (Exception e) {
            code = 1004;
        }
        return code;
    }

    public void moveNumberCount(String userNumber, HttpSession session) {
        numberCount.remove(userNumber);
        loginCount--;
        ServletContext application = session.getServletContext();
        application.setAttribute("loginCount", loginCount);
    }

    @Override
    public String getQuesByEmpNum(String empNumber) {
        if (employeesMapper.getEmpByEmpNumber(empNumber).size() == 1) {
            Employee employee = employeesMapper.getEmpByEmpNumber(empNumber).get(0);
            return employee.getQues();
        } else {
            return null;
        }
    }

    @Override
    public Boolean checkEmpAns(String empNumber, String empAns) {
        Employee employee = employeesMapper.getEmpByEmpNumber(empNumber).get(0);
        return empAns.equals(employee.getAns());
    }

    @Override
    public boolean updateEmpPassByEmpNumber(String empNumber, String newPass) {
        int res = employeesMapper.updateEmpPassByEmpNum(empNumber, newPass);
        return res == 1;
    }


}
